//
//  QLNiceCustomButton.m
//  Nice
//
//  Created by huangyueqi on 2017/7/26.
//  Copyright © 2017年 sjyt. All rights reserved.
//

#import "QLNiceCustomButton.h"

#define RGB(r, g, b) [UIColor colorWithRed:r green:g blue:b alpha:1.0]

@implementation QLNiceCustomButton

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initButtonStyle];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initButtonStyle];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initButtonStyle];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initButtonStyle];
    }
    return self;
}

- (void)initButtonStyle {
    
    self.layer.cornerRadius = 5.0;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    self.layer.borderWidth = 1.0;
    [self setButtonNormalState];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self setTitleColor:[UIColor colorWithRed:237 / 255.0 green:84 / 255.0 blue:175 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [self setBackgroundColor:[UIColor whiteColor]];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setButtonNormalState];
    });
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setButtonNormalState];
    });
    [super touchesCancelled:touches withEvent:event];
}

- (void)setButtonNormalState {
    
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self setBackgroundColor:[UIColor clearColor]];
}


@end
